<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               v-model:page="page"
               v-model:search="search"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @sort-change="sortChange"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template  #menu-left>
        <el-button type="primary" icon="el-icon-plus" v-if="permission.lcl_main_add" @click="handleAdd">新 增
        </el-button>
        <!--<el-button type="danger"-->
                   <!--icon="el-icon-delete"-->
                   <!--v-if="permission.lcl_main_delete"-->
                   <!--@click="handleDelete">删 除-->
        <!--</el-button>-->
      </template>
      <template #deliveryDate-search="{ disabled, size }">
        <!--<div style="display: flex;flex-direction: start;">-->
          <!--<el-select v-model="search.deliveryDateRule" placeholder="比较" filterable clearable>-->
            <!--<el-option label="" value=""></el-option>-->
            <!--<el-option value=">" label="大于">大于 </el-option>-->
            <!--<el-option value=">=" label="大于等于">大于等于 </el-option>-->
            <!--<el-option value="=" label="等于">等于 </el-option>-->
            <!--<el-option value="!=" label="不等于">不等于 </el-option>-->
            <!--<el-option value="<=" label="小于等于">小于等于 </el-option>-->
            <!--<el-option value="<" label="小于">小于 </el-option>-->
          <!--</el-select>-->
          <!--<el-date-picker-->
                  <!--size="default"-->
                  <!--v-model="search.deliveryDateSearch"-->
                  <!--type="date"-->
                  <!--:format="dateFormat"-->
                  <!--:value-format="dateFormat"-->
                  <!--:unlink-panels="true"-->
                  <!--clearable-->
                  <!--placeholder="出货日期"-->
                  <!--:picker-options="pickerOptions"-->
          <!--&gt;-->
          <!--</el-date-picker>-->
        <!--</div>-->
        <el-date-picker
                size="default"
                style="width:94%"
                v-model="this.deliveryDateRange"
                type="daterange"
                :format="dateFormat"
                :value-format="dateFormat"
                unlink-panels
                start-placeholder="Start Date"
                end-placeholder="End Date"
                clearable
                placeholder="出货日期"
                @change="handleDeliveryDateChange"
                :picker-options="pickerOptions"
        >
        </el-date-picker>
      </template>
      <template #menu="{ row, size, type }">
        <el-tooltip content="查看" placement="top">
          <el-button
                  icon="el-icon-view"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleView(row)"
                  v-if="permission.lcl_main_view"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="编辑" placement="top">
          <el-button
                  icon="Edit"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleEdit(row)"
                  v-if="permission.lcl_main_edit  && row.isUsed !=='是'"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="一键出仓" placement="top">
          <el-button
                  icon="Download"
                  link
                  :size="size"
                  :type="type"
                  @click="() => GenStockOut(row)"
                  v-if="permission.lcl_main_edit && row.isUsed !=='是'"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="通知单导出" placement="top">
          <el-button
                  icon="Reading"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleExport(row)"
                  >
          </el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button
                  icon="el-icon-delete"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowDel(row)"
                  v-if="permission.lcl_main_delete && row.isUsed !=='是'"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
    <lcl-form  ref="lclForm" @success="refreshChange()"/>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, genStockOut,resetStockOut} from "@/api/anwise/logistics/lcl_main";
  import {mapGetters} from "vuex";
  import lclForm from './components/lcl_form.vue';
  import { getToken } from '@/utils/auth';
  import { exportBlobPost } from '@/api/common';
  import { downloadXls } from '@/utils/util';
  import NProgress from 'nprogress';
  import 'nprogress/nprogress.css';

  export default {
      components: {
          lclForm,
      },
    data() {
      return {
        form: {},
        query: {},
          search:{},
          pickerOptions:{},
          deliveryDateRange:[],
          loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
          dateFormat:"YYYY-MM-DD",
        selectionList: [],
        option: {
            height:'auto',
            calcHeight: 30,
            tip: false,
            searchShow: false,
            searchMenuSpan: 6,
            border: true,
            index: true,
            selection: true,
            reserveSelection:true,
            labelWidth: 150,
            indexWidth:100,
            menuWidth: 200,
            addBtn: false,
            editBtn: false,
            viewBtn: false,
            delBtn: false,
            stripe: true,
            searchIndex: 3,
            searchIcon: false,
            searchEnter:true,
          column: [
            {
              label: "LCL编号",
              prop: "lclNo",
              type: "input",
              search: true,
                fixed: true,
                overHidden: true,
                sortable: true,
                width:150,
              rules: [{
                required: true,
                message: "请输入LCL编号",
                trigger: "blur"
              }],
            },
            {
              label: "装柜地址",
              prop: "loadingAddr",
              type: "input",
                overHidden: true,
                sortable: true,
            },
            {
              label: "出货日期",
              prop: "deliveryDate",
              type: "input",
                search: true,
                overHidden: true,
                sortable: true,
                width:140,
            },
            {
              label: "司机资料",
              prop: "driverInfo",
              type: "input",
                overHidden: true,
                sortable: true,
                width:150,
            },
            {
              label: "车牌",
              prop: "licenseNo",
              type: "input",
                search: true,
                overHidden: true,
                sortable: true,
                width:130,
            },
            {
              label: "创建人",
              prop: "createUserName",
              type: "input",
                overHidden: true,
                sortable: true,
                width:130,

            },
            {
              label: "创建时间",
              prop: "createTime",
              type: "input",
                overHidden: true,
                sortable: true,
                width:160,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.lcl_main_add, false),
          viewBtn: this.validData(this.permission.lcl_main_view, false),
          delBtn: this.validData(this.permission.lcl_main_delete, false),
          editBtn: this.validData(this.permission.lcl_main_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
        GenStockOut(row) {
          genStockOut(row).then(res =>{
            if(res.data.data === 201){
                      this.$message({
                          type: "error",
                          message: "已有生成的出仓单!"
                      });
                      return;
              // this.$confirm("已有生成的出仓单号，是否覆盖?", {
              //   confirmButtonText: "确定",
              //   cancelButtonText: "取消",
              //   type: "warning"
              // }).then(() => {
              //   resetStockOut(row).then(res=>{
              //     if (res.data.success) {
              //       this.$message({
              //         type: "success",
              //         message: "覆盖成功!"
              //       });
              //       this.$router.push("/anwise/stock/stock_out_main");
              //     }
              //   })
              // });
            }else if(res.data.success){
              this.$message({
                type: "success",
                message: res.data.msg||"生成成功!"
              });
              this.$router.push("/anwise/stock/stock_out_main");
            }
          })
        },
      handleExport(row){
        NProgress.start();
        exportBlobPost(
                `/blade-anwise/logistics/lcl_main/lclExcel?${this.website.tokenHeader
                }=${getToken()}`, row
        ).then(res => {
          downloadXls(res.data, `LCL通知单${this.$dayjs().format('YYYYMMDDHHmmss')}.xlsx`);
          NProgress.done();
        });
      },
        handleView(row) {
            let title="LCL详情";
            let type='view';
            const id = row.id;
            this.$refs.lclForm.show({
                id,
                title,
                type
            })
        },
        handleEdit(row) {
            let title="LCL详情";
            let type='edit';
            const id = row.id;
            this.$refs.lclForm.show({
                id,
                title,
                type
            })
        },
        handleAdd() {
            let title="LCL详情";
            let type='edit';
            this.$refs.lclForm.show({
                title,
                type
            })
        },
        sortChange(val) {
            console.log(val)
            this.sort = {
                prop: val.prop,
                order: val.order
            };
            let order = "";
            if (this.sort.prop && this.sort.order) {
                if (this.sort.order === "descending") {
                    order = `${this.sort.prop} desc`
                } else {
                    order = `${this.sort.prop} asc`
                }
            }
            this.query.orderBy = order;
            this.onLoad(this.page);
        },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("删除通知单会同步删除对应生成的出仓单，确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.search = {};
          this.deliveryDateRange = [];
          this.search.deliveryDateStart = '';
          this.search.deliveryDateEnd = '';
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
        handleDeliveryDateChange(value) {
            this.search.deliveryDateStart = value[0];
            this.search.deliveryDateEnd = value[1];
        },
    }
  };
</script>

<style>
</style>
